<?php
////
////if(!defined('IN_DISCUZ')) {
////	exit('Access Denied');
////}
////
////class weixinClass{
////
////	private $appId;
////	private $appSecret;
////	private $accessTokenCachename;
////	private $jsApiTicketCachename;
////
////	public function __construct($appId, $appSecret) {
////
////		$this->appId = trim($appId);
////		$this->appSecret = trim($appSecret);
////		$key = md5($this->appId."-".$this->appSecret);
////		$this->accessTokenCachename = 'tom_weixin_access_token_'.$key;
////		$this->jsApiTicketCachename = 'tom_weixin_js_api_ticket_'.$key;
////	}
////
////	//发送注册通知
////	public function sendRegisterMessage($user)
////	{
////		//消息模板
////		$template = "iAWeWk5O0a_8FjG-kr0saKbZBYH9O-k6FgMb6pQNZ6Q";
////		$authoInfo = C::t('#tom_love#tom_love_renzheng')->fetch_by_uid($user["bbs_uid"]);
////		//消息内容
////		$message = array(
////			"first" => array(
////				"value" => "感谢您登录读书会！",
////				"color" => "#173177",
////			),
////			"keyword1" => array(
////				"value" => mb_convert_encoding($user["nickname"], "UTF-8", "GBK"),
////				"color" => "#173177",
////			),
////			"keyword2" => array(
////				"value" => $user['tel'],
////				"color" => "#173177",
////			),
////			"keyword3" => array(
////				"value" => date("Y-m-d"),
////				"color" => "#173177",
////			),
////			"remark" => array(
////				"value" => "欢迎您加入读书会，请进入公众号点击菜单“我要入会”申请入会！",
////				"color" => "#173177",
////			),
////		);
////
////		$this->sendTemplateMessage($user, $template, $message);
////	}
////
////	//发送认证消息
////	public function sendAuthMessage($user, $type, $content = "")
////	{
////		//认证信息
////		$authoInfo = C::t('#tom_love#tom_love_renzheng')->fetch_by_uid($user["id"]);
////
////		//消息模板
////		$template = "AbHnOPfklM9ZvRz7Pd0_CZGLVrJZ3EislACGgWWnzeY";
////		//消息内容
////		$message = array();
////
////		//审核成功
////		if ($type == 1)
////		{
////			//获取审核扣费
////			$settings = C::t('#tom_love#tom_love_settings')->fetch();
////
////			$message = array(
////				"first" => array(
////					"value" => "您的身份认证申请已通过审核！并成功扣除认证费用",
////					"color" => "#173177",
////				),
////				"keyword1" => array(
////					"value" => $authoInfo["nickname"],
////					"color" => "#173177",
////				),
////				"keyword2" => array(
////					"value" => $authoInfo['bbs_username'],
////					"color" => "#173177",
////				),
////				"keyword3" => array(
////					"value" => "审核已通过！",
////					"color" => "#173177",
////				),
////				"remark" => array(
////					"value" => "感谢您的支持！",
////					"color" => "#173177",
////				),
////			);
////		}
////		//审核失败
////		else if ($type == -1)
////		{
////			$message = array(
////				"first" => array(
////					"value" => "您的身份认证申请未通过审核！",
////					"color" => "#173177",
////				),
////				"keyword1" => array(
////					"value" => $authoInfo["nickname"],
////					"color" => "#173177",
////				),
////				"keyword2" => array(
////					"value" => $authoInfo['bbs_username'],
////					"color" => "#173177",
////				),
////				"keyword3" => array(
////					"value" =>  "审核未通过！" . mb_convert_encoding($content, "UTF-8", "GBK"),
////					"color" => "#173177",
////				),
////				"remark" => array(
////					"value" => "感谢您的支持！",
////					"color" => "#173177",
////				),
////			);
////		}
////
////		$this->sendTemplateMessage($user, $template, $message);
////	}
////
////	//发送金币变更消息
////	public function sendScoreMessage($user, $money='', $reason='', $desc)
////	{
////		$userInfo = C::t('#tom_love#tom_love')->fetch_by_bbs_uid($user["bbs_uid"]);
////		//消息模板
////		$template = "nr7-DsBdxPayAYnhPM514WAaeLBRU4yNCfR3H450L3c";
////		//消息内容
////		$message = array(
////			"first" => array(
////				"value" => mb_convert_encoding($desc, "UTF-8", "GBK"),
////				"color" => "#173177",
////			),
////			"keyword1" => array(
////				"value" => $userInfo['score'],
////				"color" => "#173177",
////			),
////			"keyword2" => array(
////				"value" => date("Y年m月d日"),
////				"color" => "#173177",
////			),
////
////			"remark" => array(
////				"value" => "帐户金币可用于平台购物消费或提现到微信钱包,可通过平台菜单里的\"分享赚金币\"分享给朋友，赚取金币。",
////				"color" => "#173177",
////			),
////		);
////
////
////
////
////		$this->sendTemplateMessage($user, $template, $message);
////	}
////
////	//发送会员到期提醒消息
////	public function sendAuthDueMessage($user, $message, $time)
////	{
////		//消息模板
////		$template = "HBo_cECGPR38sWo-E5vlcAwaQxaasGFcv4hN-zpgxZk";
////		//获取审核扣费
////		$settings = C::t('#tom_love#tom_love_settings')->fetch();
////		//消息内容
////		$message = array(
////			"first" => array(
////				"value" => $message,
////				"color" => "#173177",
////			),
////			"name" => array(
////				"value" => "会员年费",
////				"color" => "#173177",
////			),
////			"expDate" => array(
////				"value" => $time,
////				"color" => "#173177",
////			),
////			"remark" => array(
////				"value" => "",
////				"color" => "#173177",
////			),
////		);
////
////		$this->sendTemplateMessage($user, $template, $message);
////	}
//////	public function sendTipMessage($user, $message) //下发给管理员信息
//////	{
//////		//消息模板
//////		$template = "TUqZn2RFHC67b2nKSXsEN1BHgnLtnG9ZD3Q0JNoqBoo";
//////		//获取审核扣费
//////		$userInfo = C::t('#tom_love#tom_love')->fetch_by_bbs_uid($user["bbs_uid"]);
//////		$userAdmin = C::t('#tom_love#tom_love')->fetch_by_bbs_uid('1641');
//////		//消息内容
//////		$message = array(
//////			"first" => array(
//////				"value" => $message,
//////				"color" => "#173177",
//////			),
//////			"accountType" => array(
//////			 'value'=>array(
//////					"account" => $userInfo["wx"] ,
//////				)
//////			),
//////			"amount" => array(
//////				"value" => '89',
//////				"color" => "#173177",
//////			),
//////			"result" => array(
//////				"value" => '充值成功',
//////				"color" => "#173177",
//////			),
//////			"remark" => array(
//////				"value" => "请及时把会员拉进群里",
//////				"color" => "#173177",
//////			),
//////		);
//////
//////		$this->sendTemplateMessage($userAdmin, $template, $message);
//////	}
////	public function sendTipMessage($user)//下发给管理员
////	{
////
////		$userInfo = C::t('#tom_love#tom_love')->fetch_by_bbs_uid($user["bbs_uid"]);
////
////
////		if($userInfo['pay_status'] == 1){
////			$name  = '普通会员';
////		}else{
////			$name  = '格局会员';
////		}
////		//消息模板
////		$template = "iAWeWk5O0a_8FjG-kr0saKbZBYH9O-k6FgMb6pQNZ6Q";
////		//消息内容
////		$message = array(
////			"first" => array(
////				"value" =>  mb_convert_encoding($userInfo['nickname'], "UTF-8", "GBK").'成功缴费加入读书会',
////				"color" => "#173177",
////			),
////			"keyword1" => array(
////				"value" => mb_convert_encoding($user["nickname"], "UTF-8", "GBK"),
////				"color" => "#173177",
////			),
////			"keyword2" => array(
////				"value" => $userInfo['wx'],
////				"color" => "#173177",
////			),
////			"keyword3" => array(
////				"value" => date('Y年m月d日',time()),
////				"color" => "#173177",
////			),
////			"remark" => array(
////				"value" => mb_convert_encoding($user["nickname"], "UTF-8", "GBK").'成为'.$name.'，请尽快拉入群',
////				"color" => "#173177",
////			),
////		);
////
////		$this->sendTemplateMessage($userInfo, $template, $message);
////	}
////
////
////	//发送提现成功消息
////	public function sendCashMessage($user, $cashInfo)
////	{
////		//消息模板
////		$template = "-tCF1gJXwbycrolgGCSluuXy10bRC47_AzRMtamEzyo";
////		//获取审核扣费
////		$settings = C::t('#tom_love#tom_love_settings')->fetch();
////		//消息内容
////		$message = array(
////			"first" => array(
////				"value" => "您提现的" . $cashInfo["cash_value"]
////					. "金币共计人民币" . $cashInfo["cash_value"] / $settings["cash_rate"]
////					. "元已成功到帐！",
////				"color" => "#173177",
////			),
////			"keyword1" => array(
////				"value" => $cashInfo["cash_value"] / $settings["cash_rate"],
////				"color" => "#173177",
////			),
////			"keyword2" => array(
////				"value" => $cashInfo["cash_time"],
////				"color" => "#173177",
////			),
////			"keyword3" => array(
////				"value" => date("Y年m月d日 H:i:s"),
////				"color" => "#173177",
////			),
////			"remark" => array(
////				"value" => "感谢您的使用！",
////				"color" => "#173177",
////			),
////		);
////
////		$this->sendTemplateMessage($user, $template, $message);
////	}
////
////	//发送奖励消息
////	public function sendRewardMessage($user, $reward, $type)
////	{
////		//消息模板
////		$template = "lUGMz0FAyePDN6MCE8uRodAFMtfoFMz7l0rEiPFoWI8";
////		//消息内容
////		$message = array();
////
////		//推荐会员
////		if ($type == 1)
////		{
////			$message = array(
////				"first" => array(
////					"value" => "推荐会员成功，系统奖励" . $reward . "元",
////					"color" => "#173177",
////				),
////				"keyword1" => array(
////					"value" => "推荐会员",
////					"color" => "#173177",
////				),
////				"keyword2" => array(
////					"value" => $reward . "元",
////					"color" => "#173177",
////				),
////				"keyword3" => array(
////					"value" => date("Y年m月d日 H:i:s"),
////					"color" => "#173177",
////				),
////				"remark" => array(
////					"value" => "感谢您的支持，请继续努力！",
////					"color" => "#173177",
////				),
////			);
////		}
////		//下线成员推荐会员
////		else if ($type == 2)
////		{
////			$message = array(
////				"first" => array(
////					"value" => "下线推荐会员成功，系统奖励" . $reward . "元",
////					"color" => "#173177",
////				),
////				"keyword1" => array(
////					"value" => "下线成员推荐会员",
////					"color" => "#173177",
////				),
////				"keyword2" => array(
////					"value" => $reward . "元",
////					"color" => "#173177",
////				),
////				"keyword3" => array(
////					"value" => date("Y年m月d日 H:i:s"),
////					"color" => "#173177",
////				),
////				"remark" => array(
////					"value" => "感谢您的支持，请继续努力！",
////					"color" => "#173177",
////				),
////			);
////		}
////
////		$this->sendTemplateMessage($user, $template, $message);
////	}
////
////	public function get_url(){
////		$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
////		$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
////
////		return $url;
////	}
////
////
////	public function get_jssdk_config() {
////
////		$jsapiTicket = $this->get_js_api_ticket();
////		$url = $this->get_url();
////
////		$timestamp = TIMESTAMP;
////		$nonceStr = $this->create_nonce_str();
////
////		$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
////
////		$signature = sha1($string);
////
////		$signConfig = array(
////			"appId"     => $this->appId,
////			"nonceStr"  => $nonceStr,
////			"timestamp" => $timestamp,
////			"url"       => $url,
////			"signature" => $signature,
////			"rawString" => $string
////		);
////		return $signConfig;
////	}
////
////	//发送模板消息
////	public function sendTemplateMessage($user, $template, $message)
////	{
////		//获取用户的openid
////		$openid = $user["openid"];
////
////		//获取access_token
////		$accessToken = $this->get_access_token();
////
////		//接口请求地址
////
////		$apiUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $accessToken;
////
////		//请求数据
////		$data = array(
////			"touser" =>$openid ,
////			"template_id" => $template,
////			"data" => $message,
////		);
////
////		$opts = array (
////			"http" => array (
////				"method" => "POST",
////				"header"=> "Content-type: application/x-www-form-urlencoded\r\n",
////				"content" => json_encode($data)
////			)
////		);
////
////		$context = stream_context_create($opts);
////		$html = file_get_contents($apiUrl, false, $context);
////		//var_dump($html);
////	}
////
//////	客户消息
////	public function sendKf($user){
////		$userInfo = C::t('#tom_love#tom_love')->fetch_by_bbs_uid($user["bbs_uid"]);
////		$adminInfo = C::t('#tom_love#tom_love_admin_book')->fetch_all_list();
////
////		$pay_status = $userInfo["pay_status"] == 1?'普通会员':'格局会员';
////
////
////		//获取access_token
////		$accessToken = $this->get_access_token();
////
////		//接口请求地址
////		$apiUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $accessToken;
////
////		foreach ($adminInfo as $k => $v) {
////
////			$data = array(
////				"touser" => $v['openid'],
////				"msgtype" => "text",
////				"text" => array(
////					"content" => mb_convert_encoding($user["nickname"], "UTF-8", "GBK") . ' 刚刚成为' . $pay_status . "，\n 微信号：" . $user['wx'] . " 请尽快拉入群。\n时间:"  . date('Y年m月d日', time()),
////				)
////			);
////			$opts = array(
////				"http" => array(
////					"method" => "POST",
////					"header" => "Content-type: application/x-www-form-urlencoded\r\n",
////					"content" => json_encode($data, JSON_UNESCAPED_UNICODE)
////				)
////			);
////			$context = stream_context_create($opts);
////			file_get_contents($apiUrl, false, $context);
////		}
////
////	}
////
//////	购买下发消息
////	public function sendGmInfo($user){
////		$userInfo = C::t('#tom_love#tom_love')->fetch_by_bbs_uid($user["bbs_uid"]);
////		$adminInfo = C::t('#tom_love#tom_love_admin_book')->fetch_all_list();
////
////		//获取access_token
////		$accessToken = $this->get_access_token();
////
////		//接口请求地址
////		$apiUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $accessToken;
////
////		foreach ($adminInfo as $k => $v) {
////
////			$data = array(
////				"touser" => $v['openid'],
////				"msgtype" => "text",
////				"text" => array(
////					"content" => mb_convert_encoding($user["nickname"], "UTF-8", "GBK") . "  刚刚购买成功；\n 微信号：" . $user['wx'] . "；\n 手机号：".$user['tel']."。\n 时间: " . date('Y年m月d日', time()),
////				)
////			);
////			$opts = array(
////				"http" => array(
////					"method" => "POST",
////					"header" => "Content-type: application/x-www-form-urlencoded\r\n",
////					"content" => json_encode($data, JSON_UNESCAPED_UNICODE)
////				)
////			);
////			$context = stream_context_create($opts);
////			file_get_contents($apiUrl, false, $context);
////		}
////
////	}
////
////
////
////	public function get_access_token(){
////		$appid = $this->appId;
////		$appsecret = $this->appSecret;
////		$cachename = $this->accessTokenCachename;
////
////		$access_token = '';
////		$cache_time = 0;
////
////		require_once libfile('function/cache');
////
////		@include(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php');
////		if(!file_exists(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php') || ($cache_time + 600 < TIMESTAMP)){
////			$get_access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
////			$return = get_html($get_access_token_url);
////			if(!empty($return)){
////				$content = json_decode($return,true);
////				if(is_array($content) && !empty($content) && isset($content['access_token']) && !empty($content['access_token'])){
////					$access_token = $content['access_token'];
////				}
////			}
////
////			$cachedata = "\$access_token='".$access_token."';\n\$cache_time='".TIMESTAMP."';\n";
////			writetocache($cachename, $cachedata);
////			@include(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php');
////		}
////		return $access_token;
////
////	}
////
////	public function get_js_api_ticket(){
////		$appid = $this->appId;
////		$appsecret = $this->appSecret;
////		$cachename = $this->jsApiTicketCachename;
////
////		$js_api_ticket = '';
////		$cache_time = 0;
////
////		require_once libfile('function/cache');
////
////		@include(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php');
////		if(!file_exists(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php') || ($cache_time + 3600 < TIMESTAMP)){
////
////			$access_token = $this->get_access_token();
////			$get_js_api_ticket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$access_token";
////			$return = get_html($get_js_api_ticket);
////			if(!empty($return)){
////				$content = json_decode($return,true);
////				if(is_array($content) && !empty($content) && isset($content['ticket']) && !empty($content['ticket'])){
////					$js_api_ticket = $content['ticket'];
////				}
////			}
////
////			$cachedata = "\$js_api_ticket='".$js_api_ticket."';\n\$cache_time='".TIMESTAMP."';\n";
////			writetocache($cachename, $cachedata);
////			@include(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php');
////		}
////		return $js_api_ticket;
////
////	}
////
////	private function create_nonce_str($length = 16) {
////		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
////		$str = "";
////		for ($i = 0; $i < $length; $i++) {
////			$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
////		}
////		return $str;
////	}
////}
////
////
////function get_html($url){
////	if(function_exists('curl_init')){
////		$ch = curl_init();
////		curl_setopt($ch, CURLOPT_URL, $url);
////		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
////		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
////		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
////		curl_setopt($ch, CURLOPT_HEADER, 0);
////		$return = curl_exec($ch);
////		curl_close($ch);
////		return $return;
////	}
////	return false;
////}
////
//if(!defined('IN_DISCUZ')) {
//	exit('Access Denied');
//}
//
//class weixinClass{
//
//	private $appId;
//	private $appSecret;
//	private $accessTokenCachename;
//	private $jsApiTicketCachename;
//
//	public function __construct($appId, $appSecret) {
//
//		$this->appId = trim($appId);
//		$this->appSecret = trim($appSecret);
//		$key = md5($this->appId."-".$this->appSecret);
//		$this->accessTokenCachename = 'tom_weixin_access_token_'.$key;
//		$this->jsApiTicketCachename = 'tom_weixin_js_api_ticket_'.$key;
//	}
//
//	//发送注册通知
//	public function sendRegisterMessage($user)
//	{
//		//消息模板
//		$template = "iAWeWk5O0a_8FjG-kr0saKbZBYH9O-k6FgMb6pQNZ6Q";
//		$authoInfo = C::t('#tom_love#tom_love_renzheng')->fetch_by_uid($user["id"]);
//		//消息内容
//		$message = array(
//			"first" => array(
//				"value" => "感谢您登录读书会！",
//				"color" => "#173177",
//			),
//			"keyword1" => array(
//				"value" => mb_convert_encoding($user["nickname"], "UTF-8", "GBK"),
//				"color" => "#173177",
//			),
//			"keyword2" => array(
//				"value" => $user['tel'],
//				"color" => "#173177",
//			),
//			"keyword3" => array(
//				"value" => date("Y-m-d"),
//				"color" => "#173177",
//			),
//			"remark" => array(
//				"value" => "欢迎您加入读书会，请进入公众号点击菜单“我要入会”申请入会！",
//				"color" => "#173177",
//			),
//		);
//
//		$this->sendTemplateMessage($user, $template, $message);
//	}
//
//	//发送认证消息
//	public function sendAuthMessage($user, $type, $content = "")
//	{
//		//认证信息
//		$authoInfo = C::t('#tom_love#tom_love_renzheng')->fetch_by_uid($user["id"]);
//
//		//消息模板
//		$template = "AbHnOPfklM9ZvRz7Pd0_CZGLVrJZ3EislACGgWWnzeY";
//		//消息内容
//		$message = array();
//
//		//审核成功
//		if ($type == 1)
//		{
//			//获取审核扣费
//			$settings = C::t('#tom_love#tom_love_settings')->fetch();
//
//			$message = array(
//				"first" => array(
//					"value" => "您的身份认证申请已通过审核！并成功扣除认证费用",
//					"color" => "#173177",
//				),
//				"keyword1" => array(
//					"value" => $authoInfo["nickname"],
//					"color" => "#173177",
//				),
//				"keyword2" => array(
//					"value" => $authoInfo['bbs_username'],
//					"color" => "#173177",
//				),
//				"keyword3" => array(
//					"value" => "审核已通过！",
//					"color" => "#173177",
//				),
//				"remark" => array(
//					"value" => "感谢您的支持！",
//					"color" => "#173177",
//				),
//			);
//		}
//		//审核失败
//		else if ($type == -1)
//		{
//			$message = array(
//				"first" => array(
//					"value" => "您的身份认证申请未通过审核！",
//					"color" => "#173177",
//				),
//				"keyword1" => array(
//					"value" => $authoInfo["nickname"],
//					"color" => "#173177",
//				),
//				"keyword2" => array(
//					"value" => $authoInfo['bbs_username'],
//					"color" => "#173177",
//				),
//				"keyword3" => array(
//					"value" =>  "审核未通过！" . mb_convert_encoding($content, "UTF-8", "GBK"),
//					"color" => "#173177",
//				),
//				"remark" => array(
//					"value" => "感谢您的支持！",
//					"color" => "#173177",
//				),
//			);
//		}
//
//		$this->sendTemplateMessage($user, $template, $message);
//	}
//
//	//发送金币变更消息
//	public function sendScoreMessage($user, $money='', $reason='', $desc)
//	{
//		$userInfo = C::t('#tom_love#tom_love')->fetch_by_bbs_uid($user["bbs_uid"]);
//		//消息模板
//		$template = "nr7-DsBdxPayAYnhPM514WAaeLBRU4yNCfR3H450L3c";
//		//消息内容
//		$message = array(
//			"first" => array(
//				"value" => mb_convert_encoding($desc, "UTF-8", "GBK"),
//				"color" => "#173177",
//			),
//			"keyword1" => array(
//				"value" => $userInfo['score'],
//				"color" => "#173177",
//			),
//			"keyword2" => array(
//				"value" => date("Y年m月d日"),
//				"color" => "#173177",
//			),
//
//			"remark" => array(
//				"value" => "帐户金币可用于平台购物消费或提现到微信钱包,可通过平台菜单里的\"分享赚金币\"分享给朋友，赚取金币。",
//				"color" => "#173177",
//			),
//		);
//
//
//
//
//		$this->sendTemplateMessage($user, $template, $message);
//	}
//
//	//发送会员到期提醒消息
//	public function sendAuthDueMessage($user, $message, $time)
//	{
//		//消息模板
//		$template = "HBo_cECGPR38sWo-E5vlcAwaQxaasGFcv4hN-zpgxZk";
//		//获取审核扣费
//		$settings = C::t('#tom_love#tom_love_settings')->fetch();
//		//消息内容
//		$message = array(
//			"first" => array(
//				"value" => $message,
//				"color" => "#173177",
//			),
//			"name" => array(
//				"value" => "会员年费",
//				"color" => "#173177",
//			),
//			"expDate" => array(
//				"value" => $time,
//				"color" => "#173177",
//			),
//			"remark" => array(
//				"value" => "",
//				"color" => "#173177",
//			),
//		);
//
//		$this->sendTemplateMessage($user, $template, $message);
//	}
////	public function sendTipMessage($user, $message) //下发给管理员信息
////	{
////		//消息模板
////		$template = "TUqZn2RFHC67b2nKSXsEN1BHgnLtnG9ZD3Q0JNoqBoo";
////		//获取审核扣费
////		$userInfo = C::t('#tom_love#tom_love')->fetch_by_bbs_uid($user["bbs_uid"]);
////		$userAdmin = C::t('#tom_love#tom_love')->fetch_by_bbs_uid('1641');
////		//消息内容
////		$message = array(
////			"first" => array(
////				"value" => $message,
////				"color" => "#173177",
////			),
////			"accountType" => array(
////			 'value'=>array(
////					"account" => $userInfo["wx"] ,
////				)
////			),
////			"amount" => array(
////				"value" => '89',
////				"color" => "#173177",
////			),
////			"result" => array(
////				"value" => '充值成功',
////				"color" => "#173177",
////			),
////			"remark" => array(
////				"value" => "请及时把会员拉进群里",
////				"color" => "#173177",
////			),
////		);
////
////		$this->sendTemplateMessage($userAdmin, $template, $message);
////	}
//	public function sendTipMessage($user)//下发给管理员
//	{
//
//		$userInfo = C::t('#tom_love#tom_love')->fetch_by_bbs_uid($user["bbs_uid"]);
//		$userAdmin = C::t('#tom_love#tom_love')->fetch_by_bbs_uid('1657');
//
//		if($userInfo['pay_status'] == 1){
//			$name  = '普通会员';
//		}else{
//			$name  = '格局会员';
//		}
//		//消息模板
//		$template = "iAWeWk5O0a_8FjG-kr0saKbZBYH9O-k6FgMb6pQNZ6Q";
//		//消息内容
//		$message = array(
//			"first" => array(
//				"value" =>  mb_convert_encoding($userInfo['nickname'], "UTF-8", "GBK").'成功缴费加入读书会',
//				"color" => "#173177",
//			),
//			"keyword1" => array(
//				"value" => mb_convert_encoding($user["nickname"], "UTF-8", "GBK"),
//				"color" => "#173177",
//			),
//			"keyword2" => array(
//				"value" => $userInfo['wx'],
//				"color" => "#173177",
//			),
//			"keyword3" => array(
//				"value" => date('Y年m月d日',time()),
//				"color" => "#173177",
//			),
//			"remark" => array(
//				"value" => mb_convert_encoding($user["nickname"], "UTF-8", "GBK").'成为'.$name.'，请尽快拉入群',
//				"color" => "#173177",
//			),
//		);
//
//		$this->sendTemplateMessage($userAdmin, $template, $message);
//	}
//
//
//
//	//发送提现成功消息
//	public function sendCashMessage($user, $cashInfo)
//	{
//		//消息模板
//		$template = "-tCF1gJXwbycrolgGCSluuXy10bRC47_AzRMtamEzyo";
//		//获取审核扣费
//		$settings = C::t('#tom_love#tom_love_settings')->fetch();
//		//消息内容
//		$message = array(
//			"first" => array(
//				"value" => "您提现的" . $cashInfo["cash_value"]
//					. "金币共计人民币" . $cashInfo["cash_value"] / $settings["cash_rate"]
//					. "元已成功到帐！",
//				"color" => "#173177",
//			),
//			"keyword1" => array(
//				"value" => $cashInfo["cash_value"] / $settings["cash_rate"],
//				"color" => "#173177",
//			),
//			"keyword2" => array(
//				"value" => $cashInfo["cash_time"],
//				"color" => "#173177",
//			),
//			"keyword3" => array(
//				"value" => date("Y年m月d日 H:i:s"),
//				"color" => "#173177",
//			),
//			"remark" => array(
//				"value" => "感谢您的使用！",
//				"color" => "#173177",
//			),
//		);
//
//		$this->sendTemplateMessage($user, $template, $message);
//	}
//
//	//发送奖励消息
//	public function sendRewardMessage($user, $reward, $type)
//	{
//		//消息模板
//		$template = "lUGMz0FAyePDN6MCE8uRodAFMtfoFMz7l0rEiPFoWI8";
//		//消息内容
//		$message = array();
//
//		//推荐会员
//		if ($type == 1)
//		{
//			$message = array(
//				"first" => array(
//					"value" => "推荐会员成功，系统奖励" . $reward . "元",
//					"color" => "#173177",
//				),
//				"keyword1" => array(
//					"value" => "推荐会员",
//					"color" => "#173177",
//				),
//				"keyword2" => array(
//					"value" => $reward . "元",
//					"color" => "#173177",
//				),
//				"keyword3" => array(
//					"value" => date("Y年m月d日 H:i:s"),
//					"color" => "#173177",
//				),
//				"remark" => array(
//					"value" => "感谢您的支持，请继续努力！",
//					"color" => "#173177",
//				),
//			);
//		}
//		//下线成员推荐会员
//		else if ($type == 2)
//		{
//			$message = array(
//				"first" => array(
//					"value" => "下线推荐会员成功，系统奖励" . $reward . "元",
//					"color" => "#173177",
//				),
//				"keyword1" => array(
//					"value" => "下线成员推荐会员",
//					"color" => "#173177",
//				),
//				"keyword2" => array(
//					"value" => $reward . "元",
//					"color" => "#173177",
//				),
//				"keyword3" => array(
//					"value" => date("Y年m月d日 H:i:s"),
//					"color" => "#173177",
//				),
//				"remark" => array(
//					"value" => "感谢您的支持，请继续努力！",
//					"color" => "#173177",
//				),
//			);
//		}
//
//		$this->sendTemplateMessage($user, $template, $message);
//	}
//
//	public function get_url(){
//		$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
//		$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
//
//		return $url;
//	}
//
//
//	public function get_jssdk_config() {
//
//		$jsapiTicket = $this->get_js_api_ticket();
//		$url = $this->get_url();
//
//		$timestamp = TIMESTAMP;
//		$nonceStr = $this->create_nonce_str();
//
//		$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
//
//		$signature = sha1($string);
//
//		$signConfig = array(
//			"appId"     => $this->appId,
//			"nonceStr"  => $nonceStr,
//			"timestamp" => $timestamp,
//			"url"       => $url,
//			"signature" => $signature,
//			"rawString" => $string
//		);
//		return $signConfig;
//	}
//
//	//发送模板消息
//	public function sendTemplateMessage($user, $template, $message)
//	{
//		//获取用户的openid
//		$openid = $user["openid"];
//
//		//获取access_token
//		$accessToken = $this->get_access_token();
//
//		//接口请求地址
//		$apiUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $accessToken;
//
//		//请求数据
//		$data = array(
//			"touser" =>$openid ,
//			"template_id" => $template,
//			"data" => $message,
//		);
//
//		$opts = array (
//			"http" => array (
//				"method" => "POST",
//				"header"=> "Content-type: application/x-www-form-urlencoded\r\n",
//				"content" => json_encode($data)
//			)
//		);
//
//		$context = stream_context_create($opts);
//		$html = file_get_contents($apiUrl, false, $context);
//		//var_dump($html);
//	}
//
////	客户消息
//	public function sendKf($user){
//		$userInfo = C::t('#tom_love#tom_love')->fetch_by_bbs_uid($user["bbs_uid"]);
//		$adminInfo = C::t('#tom_love#tom_love_admin_book')->fetch_all_list();
//
//		$pay_status = $userInfo["pay_status"] == 1?'普通会员':'格局会员';
//
//
//		//获取access_token
//		$accessToken = $this->get_access_token();
//
//		//接口请求地址
//		$apiUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $accessToken;
//
//		foreach ($adminInfo as $k => $v) {
//
//			$data = array(
//				"touser" => $v['openid'],
//				"msgtype" => "text",
//				"text" => array(
//					"content" => mb_convert_encoding($user["nickname"], "UTF-8", "GBK") . ' 刚刚成为' . $pay_status . "，\n 微信号：" . $user['wx'] . " 请尽快拉入群。\n时间:"  . date('Y年m月d日', time()),
//				)
//			);
//			$opts = array(
//				"http" => array(
//					"method" => "POST",
//					"header" => "Content-type: application/x-www-form-urlencoded\r\n",
//					"content" => json_encode($data, JSON_UNESCAPED_UNICODE)
//				)
//			);
//			$context = stream_context_create($opts);
//			file_get_contents($apiUrl, false, $context);
//		}
//
//	}
//
////	购买下发消息
//	public function sendGmInfo($user){
//		$userInfo = C::t('#tom_love#tom_love')->fetch_by_bbs_uid($user["bbs_uid"]);
//		$adminInfo = C::t('#tom_love#tom_love_admin_book')->fetch_all_list();
//
//		//获取access_token
//		$accessToken = $this->get_access_token();
//
//		//接口请求地址
//		$apiUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $accessToken;
//
//		foreach ($adminInfo as $k => $v) {
//
//			$data = array(
//				"touser" => $v['openid'],
//				"msgtype" => "text",
//				"text" => array(
//					"content" => mb_convert_encoding($user["nickname"], "UTF-8", "GBK") . "\n  刚刚购买成功；微信号：" . $user['wx'] . "；\n  手机号：".$user['tel']."。\n 时间: " . date('Y年m月d日', time()),
//				)
//			);
//			$opts = array(
//				"http" => array(
//					"method" => "POST",
//					"header" => "Content-type: application/x-www-form-urlencoded\r\n",
//					"content" => json_encode($data, JSON_UNESCAPED_UNICODE)
//				)
//			);
//			$context = stream_context_create($opts);
//			file_get_contents($apiUrl, false, $context);
//		}
//
//	}
//
//
//
//	public function get_access_token(){
//		$appid = $this->appId;
//		$appsecret = $this->appSecret;
//		$cachename = $this->accessTokenCachename;
//
//		$access_token = '';
//		$cache_time = 0;
//
//		require_once libfile('function/cache');
//
//		@include(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php');
//		if(!file_exists(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php') || ($cache_time + 600 < TIMESTAMP)){
//			$get_access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
//			$return = get_html($get_access_token_url);
//			if(!empty($return)){
//				$content = json_decode($return,true);
//				if(is_array($content) && !empty($content) && isset($content['access_token']) && !empty($content['access_token'])){
//					$access_token = $content['access_token'];
//				}
//			}
//
//			$cachedata = "\$access_token='".$access_token."';\n\$cache_time='".TIMESTAMP."';\n";
//			writetocache($cachename, $cachedata);
//			@include(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php');
//		}
//		return $access_token;
//
//	}
//
//	public function get_js_api_ticket(){
//		$appid = $this->appId;
//		$appsecret = $this->appSecret;
//		$cachename = $this->jsApiTicketCachename;
//
//		$js_api_ticket = '';
//		$cache_time = 0;
//
//		require_once libfile('function/cache');
//
//		@include(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php');
//		if(!file_exists(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php') || ($cache_time + 3600 < TIMESTAMP)){
//
//			$access_token = $this->get_access_token();
//			$get_js_api_ticket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$access_token";
//			$return = get_html($get_js_api_ticket);
//			if(!empty($return)){
//				$content = json_decode($return,true);
//				if(is_array($content) && !empty($content) && isset($content['ticket']) && !empty($content['ticket'])){
//					$js_api_ticket = $content['ticket'];
//				}
//			}
//
//			$cachedata = "\$js_api_ticket='".$js_api_ticket."';\n\$cache_time='".TIMESTAMP."';\n";
//			writetocache($cachename, $cachedata);
//			@include(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php');
//		}
//		return $js_api_ticket;
//
//	}
//
//	private function create_nonce_str($length = 16) {
//		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
//		$str = "";
//		for ($i = 0; $i < $length; $i++) {
//			$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
//		}
//		return $str;
//	}
//}
//
//
//function get_html($url){
//	if(function_exists('curl_init')){
//		$ch = curl_init();
//		curl_setopt($ch, CURLOPT_URL, $url);
//		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//		curl_setopt($ch, CURLOPT_HEADER, 0);
//		$return = curl_exec($ch);
//		curl_close($ch);
//		return $return;
//	}
//	return false;
//}

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class weixinClass{

	private $appId;
	private $appSecret;
	private $accessTokenCachename;
	private $jsApiTicketCachename;

	public function __construct($appId, $appSecret) {

		$this->appId = trim($appId);
		$this->appSecret = trim($appSecret);
		$key = md5($this->appId."-".$this->appSecret);
		$this->accessTokenCachename = 'tom_weixin_access_token_'.$key;
		$this->jsApiTicketCachename = 'tom_weixin_js_api_ticket_'.$key;
	}

	//发送注册通知
	public function sendRegisterMessage($user)
	{
		//消息模板
		$template = "hhQ7i1Zd2Re8LeQq6fGvnzLt5rdTHn7SarsKPLW57O8";
		$authoInfo = C::t('#tom_love#tom_love_renzheng')->fetch_by_uid($user["id"]);
		//消息内容
		$message = array(
			"first" => array(
				"value" => "感谢您登录读书会！",
				"color" => "#173177",
			),
			"keyword1" => array(
				"value" => mb_convert_encoding($user["nickname"], "UTF-8", "GBK"),
				"color" => "#173177",
			),
			"keyword2" => array(
				"value" => date("Y-m-d"),
				"color" => "#173177",
			),
			"remark" => array(
				"value" => "欢迎您加入读书会，请进入公众号点击菜单“我要入会”申请入会！",
				"color" => "#173177",
			),
		);

		$this->sendTemplateMessage($user, $template, $message);
	}

	//发送认证消息
	public function sendAuthMessage($user, $type, $content = "")
	{
		//认证信息
		$authoInfo = C::t('#tom_love#tom_love_renzheng')->fetch_by_uid($user["id"]);

		//消息模板
		$template = "AbHnOPfklM9ZvRz7Pd0_CZGLVrJZ3EislACGgWWnzeY";
		//消息内容
		$message = array();

		//审核成功
		if ($type == 1)
		{
			//获取审核扣费
			$settings = C::t('#tom_love#tom_love_settings')->fetch();

			$message = array(
				"first" => array(
					"value" => "您的身份认证申请已通过审核！并成功扣除认证费用",
					"color" => "#173177",
				),
				"keyword1" => array(
					"value" => $authoInfo["nickname"],
					"color" => "#173177",
				),
				"keyword2" => array(
					"value" => $authoInfo['bbs_username'],
					"color" => "#173177",
				),
				"keyword3" => array(
					"value" => "审核已通过！",
					"color" => "#173177",
				),
				"remark" => array(
					"value" => "感谢您的支持！",
					"color" => "#173177",
				),
			);
		}
		//审核失败
		else if ($type == -1)
		{
			$message = array(
				"first" => array(
					"value" => "您的身份认证申请未通过审核！",
					"color" => "#173177",
				),
				"keyword1" => array(
					"value" => $authoInfo["nickname"],
					"color" => "#173177",
				),
				"keyword2" => array(
					"value" => $authoInfo['bbs_username'],
					"color" => "#173177",
				),
				"keyword3" => array(
					"value" =>  "审核未通过！" . mb_convert_encoding($content, "UTF-8", "GBK"),
					"color" => "#173177",
				),
				"remark" => array(
					"value" => "感谢您的支持！",
					"color" => "#173177",
				),
			);
		}

		$this->sendTemplateMessage($user, $template, $message);
	}

	//发送金币变更消息
//	public function sendScoreMessage($user, $money='', $reason='', $desc)
//	{
//		$userInfo = C::t('#tom_love#tom_love')->fetch_by_bbs_uid($user["bbs_uid"]);
//		//消息模板
//		$template = "nr7-DsBdxPayAYnhPM514WAaeLBRU4yNCfR3H450L3c";
//		//消息内容
//		$message = array(
//			"first" => array(
//				"value" => mb_convert_encoding($desc, "UTF-8", "GBK"),
//				"color" => "#173177",
//			),
//			"keyword1" => array(
//				"value" => $userInfo['score'],
//				"color" => "#173177",
//			),
//			"keyword2" => array(
//				"value" => date("Y年m月d日"),
//				"color" => "#173177",
//			),
//
//			"remark" => array(
//				"value" => "帐户金币可用于平台购物消费或提现到微信钱包,可通过平台菜单里的\"分享赚金币\"分享给朋友，赚取金币。",
//				"color" => "#173177",
//			),
//		);
//
//
//
//
//		$this->sendTemplateMessage($user, $template, $message);
//	}

	//发送会员到期提醒消息
	public function sendAuthDueMessage($user, $message, $time)
	{
		//消息模板
		$template = "HBo_cECGPR38sWo-E5vlcAwaQxaasGFcv4hN-zpgxZk";
		//获取审核扣费
		$settings = C::t('#tom_love#tom_love_settings')->fetch();
		//消息内容
		$message = array(
			"first" => array(
				"value" => $message,
				"color" => "#173177",
			),
			"name" => array(
				"value" => "会员年费",
				"color" => "#173177",
			),
			"expDate" => array(
				"value" => $time,
				"color" => "#173177",
			),
			"remark" => array(
				"value" => "",
				"color" => "#173177",
			),
		);

		$this->sendTemplateMessage($user, $template, $message);
	}
//	public function sendTipMessage($user, $message) //下发给管理员信息
//	{
//		//消息模板
//		$template = "TUqZn2RFHC67b2nKSXsEN1BHgnLtnG9ZD3Q0JNoqBoo";
//		//获取审核扣费
//		$userInfo = C::t('#tom_love#tom_love')->fetch_by_bbs_uid($user["bbs_uid"]);
//		$userAdmin = C::t('#tom_love#tom_love')->fetch_by_bbs_uid('1641');
//		//消息内容
//		$message = array(
//			"first" => array(
//				"value" => $message,
//				"color" => "#173177",
//			),
//			"accountType" => array(
//			 'value'=>array(
//					"account" => $userInfo["wx"] ,
//				)
//			),
//			"amount" => array(
//				"value" => '89',
//				"color" => "#173177",
//			),
//			"result" => array(
//				"value" => '充值成功',
//				"color" => "#173177",
//			),
//			"remark" => array(
//				"value" => "请及时把会员拉进群里",
//				"color" => "#173177",
//			),
//		);
//
//		$this->sendTemplateMessage($userAdmin, $template, $message);
//	}
	public function sendTipMessage($user)//下发给管理员
	{

		$userInfo = C::t('#tom_love#tom_love')->fetch_by_bbs_uid($user["bbs_uid"]);
		$userAdmin = C::t('#tom_love#tom_love')->fetch_by_bbs_uid('1657');

		if($userInfo['pay_status'] == 1){
			$name  = '普通会员';
		}else{
			$name  = '格局会员';
		}
		//消息模板
		$template = "iAWeWk5O0a_8FjG-kr0saKbZBYH9O-k6FgMb6pQNZ6Q";
		//消息内容
		$message = array(
			"first" => array(
				"value" =>  mb_convert_encoding($userInfo['nickname'], "UTF-8", "GBK").'成功缴费加入读书会',
				"color" => "#173177",
			),
			"keyword1" => array(
				"value" => mb_convert_encoding($user["nickname"], "UTF-8", "GBK"),
				"color" => "#173177",
			),
			"keyword2" => array(
				"value" => $userInfo['wx'],
				"color" => "#173177",
			),
			"keyword3" => array(
				"value" => date('Y年m月d日',time()),
				"color" => "#173177",
			),
			"remark" => array(
				"value" => mb_convert_encoding($user["nickname"], "UTF-8", "GBK").'成为'.$name.'，请尽快拉入群',
				"color" => "#173177",
			),
		);

		$this->sendTemplateMessage($userAdmin, $template, $message);
	}



	//发送提现成功消息
	public function sendCashMessage($user, $cashInfo)
	{
		//消息模板
		$template = "-tCF1gJXwbycrolgGCSluuXy10bRC47_AzRMtamEzyo";
		//获取审核扣费
		$settings = C::t('#tom_love#tom_love_settings')->fetch();
		//消息内容
		$message = array(
			"first" => array(
				"value" => "您提现的" . $cashInfo["cash_value"]
					. "金币共计人民币" . $cashInfo["cash_value"] / $settings["cash_rate"]
					. "元已成功到帐！",
				"color" => "#173177",
			),
			"keyword1" => array(
				"value" => $cashInfo["cash_value"] / $settings["cash_rate"],
				"color" => "#173177",
			),
			"keyword2" => array(
				"value" => $cashInfo["cash_time"],
				"color" => "#173177",
			),
			"keyword3" => array(
				"value" => date("Y年m月d日"),
				"color" => "#173177",
			),
			"remark" => array(
				"value" => "感谢您的使用！",
				"color" => "#173177",
			),
		);

		$this->sendTemplateMessage($user, $template, $message);
	}

	//发送奖励消息
	public function sendRewardMessage($user, $reward, $type)
	{
		//消息模板
		$template = "lUGMz0FAyePDN6MCE8uRodAFMtfoFMz7l0rEiPFoWI8";
		//消息内容
		$message = array();

		//推荐会员
		if ($type == 1)
		{
			$message = array(
				"first" => array(
					"value" => "推荐会员成功，系统奖励" . $reward . "元",
					"color" => "#173177",
				),
				"keyword1" => array(
					"value" => "推荐会员",
					"color" => "#173177",
				),
				"keyword2" => array(
					"value" => $reward . "元",
					"color" => "#173177",
				),
				"keyword3" => array(
					"value" => date("Y年m月d日"),
					"color" => "#173177",
				),
				"remark" => array(
					"value" => "感谢您的支持，请继续努力！",
					"color" => "#173177",
				),
			);
		}
		//下线成员推荐会员
		else if ($type == 2)
		{
			$message = array(
				"first" => array(
					"value" => "下线推荐会员成功，系统奖励" . $reward . "元",
					"color" => "#173177",
				),
				"keyword1" => array(
					"value" => "下线成员推荐会员",
					"color" => "#173177",
				),
				"keyword2" => array(
					"value" => $reward . "元",
					"color" => "#173177",
				),
				"keyword3" => array(
					"value" => date("Y年m月d日"),
					"color" => "#173177",
				),
				"remark" => array(
					"value" => "感谢您的支持，请继续努力！",
					"color" => "#173177",
				),
			);
		}

		$this->sendTemplateMessage($user, $template, $message);
	}

	public function get_url(){
		$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
		$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

		return $url;
	}


	public function get_jssdk_config() {

		$jsapiTicket = $this->get_js_api_ticket();
		$url = $this->get_url();

		$timestamp = TIMESTAMP;
		$nonceStr = $this->create_nonce_str();

		$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

		$signature = sha1($string);

		$signConfig = array(
			"appId"     => $this->appId,
			"nonceStr"  => $nonceStr,
			"timestamp" => $timestamp,
			"url"       => $url,
			"signature" => $signature,
			"rawString" => $string
		);
		return $signConfig;
	}

	//发送模板消息
	public function sendTemplateMessage($user, $template, $message)
	{
		//获取用户的openid
		$openid = $user["openid"];

		//获取access_token
		$accessToken = $this->get_access_token();

		//接口请求地址
		$apiUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $accessToken;

		//请求数据
		$data = array(
			"touser" =>$openid ,
			"template_id" => $template,
			"data" => $message,
		);

		$opts = array (
			"http" => array (
				"method" => "POST",
				"header"=> "Content-type: application/x-www-form-urlencoded\r\n",
				"content" => json_encode($data)
			)
		);

		$context = stream_context_create($opts);
		$html = file_get_contents($apiUrl, false, $context);
		//var_dump($html);
	}

//	客户消息
	public function sendKf($user){
		$userInfo = C::t('#tom_love#tom_love')->fetch_by_bbs_uid($user["bbs_uid"]);
		$adminInfo = C::t('#tom_love#tom_love_admin_book')->fetch_all_list();

		$pay_status = $userInfo["pay_status"] == 1?'普通会员':'格局会员';

//var_dump($user);die;
		//获取access_token
		$accessToken = $this->get_access_token();

		//接口请求地址
		$apiUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $accessToken;

		foreach ($adminInfo as $k => $v) {

			$data = array(
				"touser" => $v['openid'],
				"msgtype" => "text",
				"text" => array(
					"content" => mb_convert_encoding($user["nickname"], "UTF-8", "GBK") . ' 刚刚成为' . $pay_status . "，\n 微信号：" . $userInfo['wx'] . ", 请尽快拉入群。\n时间:"  . date('Y年m月d日', time()),
				)
			);
			$opts = array(
				"http" => array(
					"method" => "POST",
					"header" => "Content-type: application/x-www-form-urlencoded\r\n",
					"content" => json_encode($data, JSON_UNESCAPED_UNICODE)
				)
			);
			$context = stream_context_create($opts);
			file_get_contents($apiUrl, false, $context);
		}

	}

//	购买下发消息
	public function sendGmInfo($user){
		$userInfo = C::t('#tom_love#tom_love')->fetch_by_bbs_uid($user["bbs_uid"]);
		$adminInfo = C::t('#tom_love#tom_love_admin_book')->fetch_all_list();

		//获取access_token
		$accessToken = $this->get_access_token();

		//接口请求地址
		$apiUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $accessToken;

		foreach ($adminInfo as $k => $v) {

			$data = array(
				"touser" => $v['openid'],
				"msgtype" => "text",
				"text" => array(
					"content" => mb_convert_encoding($user["nickname"], "UTF-8", "GBK") . "  刚刚购买成功；\n 微信号：" . $user['wx'] . "；\n 手机号：".$user['tel']."。\n 时间: " . date('Y年m月d日', time()),
				)
			);
			$opts = array(
				"http" => array(
					"method" => "POST",
					"header" => "Content-type: application/x-www-form-urlencoded\r\n",
					"content" => json_encode($data, JSON_UNESCAPED_UNICODE)
				)
			);
			$context = stream_context_create($opts);
			file_get_contents($apiUrl, false, $context);
		}

	}



	public function get_access_token(){
		$appid = $this->appId;
		$appsecret = $this->appSecret;
		$cachename = $this->accessTokenCachename;

		$access_token = '';
		$cache_time = 0;

		require_once libfile('function/cache');

		@include(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php');
		if(!file_exists(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php') || ($cache_time + 600 < TIMESTAMP)){
			$get_access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
			$return = get_html($get_access_token_url);
			if(!empty($return)){
				$content = json_decode($return,true);
				if(is_array($content) && !empty($content) && isset($content['access_token']) && !empty($content['access_token'])){
					$access_token = $content['access_token'];
				}
			}

			$cachedata = "\$access_token='".$access_token."';\n\$cache_time='".TIMESTAMP."';\n";
			writetocache($cachename, $cachedata);
			@include(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php');
		}
		return $access_token;

	}

	public function sendScoreMessage($user, $money, $reason, $desc)
	{
		$userInfo = C::t('#tom_love#tom_love')->fetch_by_bbs_uid($user["bbs_uid"]);
		//消息模板
		$template = "KBivZS7eyqMfMn-M4XVyhjwI8hZ8GZpJasMZjMcN00g";
		//消息内容
		$message = array(
			"first" => array(
				"value" => mb_convert_encoding($desc, "UTF-8", "GBK"),
				"color" => "#173177",
			),
			"keyword1" => array(
				"value" => mb_convert_encoding($user["nickname"], "UTF-8", "GBK"),
				"color" => "#173177",
			),
			"keyword2" => array(
				"value" => date("Y年m月d日"),
				"color" => "#173177",
			),
			"keyword3" => array(
				"value" => $money,
				"color" => "#173177",
			),
			"keyword4" => array(
				"value" => $userInfo["score"],
				"color" => "#173177",
			),
			"keyword5" => array(
				"value" => mb_convert_encoding($reason, "UTF-8", "GBK"),
				"color" => "#173177",
			),
			"remark" => array(
				"value" => "帐户金币可用于平台购物消费或提现到微信钱包,可通过平台菜单里的\"分享赚金币\"分享给朋友，赚取金币。",
				"color" => "#173177",
			),
		);

		$this->sendTemplateMessage($user, $template, $message);
	}

	public function get_js_api_ticket(){
		$appid = $this->appId;
		$appsecret = $this->appSecret;
		$cachename = $this->jsApiTicketCachename;

		$js_api_ticket = '';
		$cache_time = 0;

		require_once libfile('function/cache');

		@include(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php');
		if(!file_exists(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php') || ($cache_time + 3600 < TIMESTAMP)){

			$access_token = $this->get_access_token();
			$get_js_api_ticket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$access_token";
			$return = get_html($get_js_api_ticket);
			if(!empty($return)){
				$content = json_decode($return,true);
				if(is_array($content) && !empty($content) && isset($content['ticket']) && !empty($content['ticket'])){
					$js_api_ticket = $content['ticket'];
				}
			}

			$cachedata = "\$js_api_ticket='".$js_api_ticket."';\n\$cache_time='".TIMESTAMP."';\n";
			writetocache($cachename, $cachedata);
			@include(DISCUZ_ROOT.'./data/sysdata/cache_'.$cachename.'.php');
		}
		return $js_api_ticket;

	}

	private function create_nonce_str($length = 16) {
		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		$str = "";
		for ($i = 0; $i < $length; $i++) {
			$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
		}
		return $str;
	}
}


function get_html($url){
	if(function_exists('curl_init')){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		$return = curl_exec($ch);
		curl_close($ch);
		return $return;
	}
	return false;
}

